package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.pojo.Tag;
import com.blog.service.ArticleService;
import com.blog.service.TagService;
import com.blog.service.impl.ArticleServiceImpl;
import com.blog.service.impl.TagServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/TagServlet.do")
public class TagServlet extends HttpServlet {

    TagService tagService=new TagServiceImpl();
    ArticleService articleService=new ArticleServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //存储的是TAG和分类文章集合的对应关系
        Map<String, List<Article>> map=new HashMap<String, List<Article>>();
        //所有的tag标签获取 去重
        List<Tag> tags = tagService.getTags();
        for (Tag tag : tags) {
            String tagName = tag.getTag();
            List<Article> articleList = articleService.getArticlesByTagName(tagName);
            map.put(tagName,articleList);
        }
        req.setAttribute("id_tag_map",map);
        req.getRequestDispatcher("/page/tags.jsp").forward(req,resp);

    }
}
